package com.gzsxy.lesson01;


import java.io.*;
import java.util.ArrayList;

/**
 * 将字符输入流写入文本
 */
public class FileWriterTest {


    public static void main(String[] args) throws Exception {
//        // true表示不覆盖原来的内容，而是加到文件的后面。若要覆盖原来的内容，直接省略这个参数就好
//        FileWriter fileWriter = new FileWriter("D:\\dirA\\text1.txt",true);
//        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
//        bufferedWriter.newLine();
//        bufferedWriter.write("I LOVE YOU ");
//        bufferedWriter.newLine();
//        bufferedWriter.write("I LOVE YOU ");
//        bufferedWriter.close();
//        fileWriter.close();


//        FileReader fileReader = new FileReader("D:\\dirA\\text1.txt");
//        BufferedReader bufferedReader = new BufferedReader(fileReader);
//        String line= null;
////        System.out.println(bufferedReader.readLine());
//        int len = 1;
//        while ((line = bufferedReader.readLine()) != null){
//            System.out.println("len"+len+":"+line);
//            len++;
//        }
//        bufferedReader.close();
//        fileReader.close();

        File file = new File("D:\\dirA\\text1.txt");
        //如果文件存在就继续
        if (file.exists()) {
            setText(file, "ni hao", "hello word");
        }
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        ArrayList<String> strings = new ArrayList<>();
        String str = "";
        while ((str = bufferedReader.readLine()) != null && str.length() != 0) {
            strings.add(str);
        }

        if (strings.contains("xiaolong1")) {
            System.out.println("xxxx");
        }


        System.out.println(strings);


    }


    public static void setText(File fileText, String target, String src) {
        StringBuffer buffer = new StringBuffer();
        BufferedReader br = null;
        PrintWriter pw = null;
        try {
            br = new BufferedReader(new FileReader(fileText));

//        System.out.println(bufferedReader.readLine());
            //java中存在一些转义字符,比如"\n"为换行符, JDK自带的一些操作符
            String line = System.getProperty("line.separator");
//            for (String str=br.readLine();str!=null;str = br.readLine()){
            String str = null;
            while ((str = br.readLine()) != null) {
                //若target再str里面为ture
                if (str.contains(target)) {
                    str = str.replaceAll(target, src);
                }
                buffer.append(str + line);
            }
            //ture 表示当前pw具有自动行刷新的功能
            pw = new PrintWriter(new FileWriter(fileText), true);
            pw.println(buffer);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (pw != null) {
                pw.close();
            }
        }

    }


}
